/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DatabaseConnection;

import Entities.Bus;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 * null
 * @author serhat
 */
public class BusDbConnection extends Connector {
    
    
    public BusDbConnection() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
        
    }
    
    private String buildInsertQuery(Bus bus) {
        String query = String.format("INSERT INTO Otobus "
                + "VALUES ('%s', '%d', '%d',"
                + "'%s', '%s', '%d')",
                bus.getPlaka(),
                bus.getSeats(),
                bus.getStatus(),
                bus.getBrand(),
                bus.getModel(),
                bus.getYear());
        return query;
    }

    private String buildSelectQuery(String fields[], int status) {

        String query = "SELECT";
        if (fields != null) {
            for (int i = 0; i < fields.length; i++) {
                query += " " + fields[i];
                if (i != fields.length - 1) {
                    query += ",";
                }
            }
        } else {
            query += " *";
        }
        System.out.println(query);
        query += " FROM Otobus";
          
        if (status != -1){
            query += " WHERE Durum = '" + status + "'";
        }
        System.out.println(query);
        return query;
    }

    public ResultSet select(String fields[], int status) {
        try {
            String query = buildSelectQuery(fields, status);
            ResultSet rs = executeQuery(query);
            return rs;
        } catch (SQLException ex) {
            Logger.getLogger(PlaceDbConnection.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    public ResultSet select(String plaka){
        try {
            String query = "Select KoltukSayısı from Otobus where plaka = '" + plaka + "'"; 
            ResultSet rs = executeQuery(query);
            return rs;
        } catch (SQLException ex) {
            Logger.getLogger(PlaceDbConnection.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    public boolean insert(Bus bus) {

        try {
            String query = buildInsertQuery(bus);
            executeUpdate(query);
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeDbConnection.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return true;

    }
}
